<?php
/**
 * Created by XM·Tan.
 * Motto: Who shuts love out, in turn shall be shut out from love.
 * Date: 2021/3/22 22:15
 */

namespace app\admin\validate;


use think\Validate;

class AdminUser extends Validate {

    protected $rule = [
        'username' => 'require|length:5,30',
        'password' => 'require',
        'phone' => 'mobile|number|length:11',
        'captcha'  => 'require|checkCaptcha',
        'old_password' => 'require',
        'again_password' => 'require',
    ];

    protected $message = [
        'username.require' => '用户名必须填写',
        'username.length'  => '用户名为 5-30 个字符',
        'username' => '请输入用户名',
        'password' => '请输入密码',
        'captcha'  => '请输入验证码',
        'old_password' => '请输入旧密码',
        'again_password' => '请输入确认密码',
        'tel.mobile'  => '填写有效的手机',
        'tel.number'  => '手机只能数字',
        'tel.length'  => '手机只能11位数字',
    ];

    protected $scene = [
        'login'   =>  ['username', 'password', 'captcha'],
        'insert'  =>  ['username' ,'password', 'phone'],
        'update'  =>  ['username', 'phone'],
        'setting' =>  ['username', 'phone'],
        'password' => ['password', 'old_password', 'again_password'],
    ];

    /**
     * 自定义 Validate 验证码校验函数
     * @param $value
     * @param $rule
     * @param array $data
     * @return bool|string
     */
    protected function checkCaptcha($value, $rule, $data = []) {
        if (!captcha_check($value)) {
            //验证码校验失败
            return '您输入的验证码不正确！';
        }
        return true;
    }

}